package com.glsc.ngateway.platform.service.glres;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscNetInfoResource;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.glres.GlscNetInfoResourceRepository;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
public class GlscNetInfoResourceService {

    @Resource
    private GlscNetInfoResourceRepository glscNetInfoResourceRepository;

    @Resource
    private UserService userService;

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<GlscNetInfoResource> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<GlscNetInfoResource> page = glscNetInfoResourceRepository.findAll(SpecificationUtil.buildSpecification(param, GlscNetInfoResource.class), pageable);
        return page;
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public GlscNetInfoResource findById(Integer id) {
        Optional<GlscNetInfoResource> optional = glscNetInfoResourceRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 根据ip查询网络信息
     *
     * @param ip
     * @return
     */
    public GlscNetInfoResource findByIp(String ip) {
        List<GlscNetInfoResource> netInfos = glscNetInfoResourceRepository.findAll(Example.of(GlscNetInfoResource.builder().address(ip).build()));
        return CollectionUtils.isNotEmpty(netInfos) ? netInfos.get(0) : null;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @Transactional
    public GlscNetInfoResource save(GlscNetInfoResource obj) {
        if (Objects.isNull(obj.getId())) {
            GlscNetInfoResource resource = findByIp(obj.getAddress());
            if (Objects.nonNull(resource)) {
                Assert.isTrue(false, "[" + obj.getAddress() + "]网络信息已被服务器管理员[" + resource.getUserName() + "]维护，如有需求，请联系管理员");
            }
        }

        return glscNetInfoResourceRepository.save(obj);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void delete(Integer id) {
        glscNetInfoResourceRepository.deleteById(id);
    }
}
